package be.phl.layoutAdapter;

import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.TransitionDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import be.phl.model.DBAdapterScreening;
import be.phl.obstacleapp.R;

public class SingleItemProblemRowAdapter extends ArrayAdapter<ItemProblemRow> {
	/**
	 * TODO: add comment
	 */

	private static final String TAG = "SingleItemProblemRowAdapter";

	private final Context context;
	List<ItemProblemRow> ItemProblems;

	public SingleItemProblemRowAdapter(Context context, int ScreeningItemId,
			List<ItemProblemRow> itemProblems) {
		super(context, R.layout.item_problem_row, itemProblems);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.ItemProblems = itemProblems;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View rowView = inflater.inflate(R.layout.item_problem_row,
				parent, false);
		rowView.setLayoutParams(new AbsListView.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

		TextView name = (TextView) rowView.findViewById(R.id.textView1);

		// get the corresponding item
		final ItemProblemRow itemProblem = ItemProblems.get(position);
		name.setText(itemProblem.getName());

		// set the layout to fit the new view
		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) name
				.getLayoutParams();
		params.setMargins(30 + 50 * itemProblem.getNode(), 15, 0, 15);
		name.setLayoutParams(params);

		// if the item is at the end of a branch create a listener to save to
		// the database
		if (itemProblem.isBranchEnd()) {
			name.setTypeface(Typeface.DEFAULT_BOLD);
			rowView.setBackgroundResource(R.drawable.transition);

			// if the is already selected, change the state to color red
			if (itemProblem.isChecked()) {
				// animate the transition
				TransitionDrawable transition = (TransitionDrawable) rowView
						.getBackground();
				transition.startTransition(0);
			}

			rowView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int shortAnimTime = context.getResources().getInteger(
							android.R.integer.config_shortAnimTime);
					itemProblem.setChecked(!itemProblem.isChecked());

					// animate the color change
					TransitionDrawable transition = (TransitionDrawable) rowView
							.getBackground();
					if (itemProblem.isChecked()) {
						transition.startTransition(shortAnimTime);
					} else {
						transition.reverseTransition(shortAnimTime);
					}

					// save change to database
					DBAdapterScreening db = new DBAdapterScreening(context);
					db.open();
					if (itemProblem.isChecked()) {
						ItemProblems.get(position).setScreeningProblemId(
								(int) db.insertScreeningProblem(
										itemProblem.getScreeningItemId(),
										itemProblem.getId(), null));
						Log.i(TAG,
								"ScreeningProblemId: "
										+ ItemProblems.get(position)
												.getScreeningProblemId());
					} else {
						db.deleteScreeningProblem(itemProblem
								.getScreeningProblemId());
					}
					db.close();
				}

			});
		}

		return rowView;
	}

}
